package org.qiuqiu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.json.JSONObject;
import org.qiuqiu.dao.GroupDao;
import org.qiuqiu.pojo.Group;
import org.qiuqiu.pojo.User;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 杜秋野
 * 
 */
public class GroupAction extends ActionSupport implements ServletResponseAware,
		ServletRequestAware {
	HttpServletRequest request;
	HttpServletResponse response;
	private int groupId;
	private Group group = new Group();
	private String list;

	public String getList() {
		return list;
	}

	public void setList(String list) {
		this.list = list;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	/**
	 * 获得某个注册用户的所有的群组信息
	 * 
	 * @throws IOException
	 * @throws SQLException
	 */
	public void getGroupByUserId() throws IOException, SQLException {
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		GroupDao gd = new GroupDao();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		List<HashMap> groups = gd.getGroupInfo(user.getUserId(), 0);
		String result = "";
		for (HashMap group : groups) {
			JSONObject jsonObject = new JSONObject(group);
			result += jsonObject.toString();
			result += ";";
		}
		if (result.length() != 0) {
			result = result.substring(0, result.length() - 1);
		}
		out.print(result);
	}

	/**
	 * 获得指定群组的信息
	 * 
	 * @throws IOException
	 * @throws SQLException
	 */
	public void getGroupByGroupId() throws IOException, SQLException {
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		GroupDao gd = new GroupDao();
		List<HashMap> groups = gd.getGroupInfo(0, groupId);
		String result = "";
		for (HashMap group : groups) {
			JSONObject jsonObject = new JSONObject(group);
			result += jsonObject.toString();
			result += ";";
		}
		if (result.length() != 0) {
			result = result.substring(0, result.length() - 1);
		}
		out.print(result);
	}

	/**
	 * 检测群组名是否重复
	 * 
	 * @throws IOException
	 * @throws SQLException
	 */
	public void checkExistedGroupName() throws IOException, SQLException {
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		group.setUserId(user.getUserId());
		group.setGroupName(java.net.URLDecoder.decode(group.getGroupName(),
				"UTF-8"));
		GroupDao gd = new GroupDao();
		if (gd.getGroup(group) == null) {
			out.print("true");
		} else {
			out.print("false");
		}
	}

	/**
	 * 增加新群组
	 * 
	 * @throws IOException
	 * @throws SQLException
	 */
	public void addGroup() throws IOException, SQLException {
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		group.setUserId(user.getUserId());
		GroupDao gd = new GroupDao();
		if (gd.addGroup(group)) {
			out.print("true");
		} else {
			out.print("false");
		}
	}

	/**
	 * 删除指定的群组
	 * 
	 * @throws IOException
	 * @throws SQLException
	 */
	public void delGroupByGroupId() throws IOException, SQLException {
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		group.setUserId(user.getUserId());
		group.setGroupName("我的联系人");
		GroupDao gd = new GroupDao();
		int newGroup = gd.getGroup(group).getGroupId();
		String[] arr = list.split(",");
		List<Integer> fordwardList = new ArrayList<Integer>();
		for (String str : arr) {
			fordwardList.add(Integer.parseInt(str));
		}
		if (fordwardList.indexOf(newGroup) == -1) {
			for (Integer oldGroup : fordwardList) {
				gd.moveGroup(oldGroup, newGroup);
			}
			gd.delGroup(fordwardList);
			out.print("true");
		} else {
			out.print("false");
		}
	}

	/**
	 * 修改群组信息
	 * 
	 * @throws IOException
	 * @throws SQLException
	 */
	public void modGroup() throws IOException, SQLException {
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		GroupDao gd = new GroupDao();
		if (gd.modGroup(group)) {
			out.print("true");
		} else {
			out.print("false");
		}
	}
}
